<template>
<div class="app-container">
    <!-- 查询功能 -->
         <el-form :inline="true" :model="query" size="mini">
            <el-form-item label="广告标题:">
                <el-input v-model.trim="query.title"></el-input>
            </el-form-item>
            <el-form-item label="状态:">
                <el-select v-model="query.status" clearable filterable style="width:85px">
                <el-option
                    v-for="item in statusOptions"
                    :key="item.code" 
                    :label="item.name" 
                    :value="item.code">
                 </el-option>
               </el-select>
            </el-form-item>
            <el-form-item>
                <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
                <el-button  icon="el-icon-refresh" @click="reload">重置</el-button>
                <el-button  icon="el-icon-plus" type="primary"  @click="openAdd"> 新增</el-button>
            </el-form-item>
        </el-form>
        <!-- 表格主体 -->
        <el-table :data="list" stripe style="width: 100%">
            <el-table-column  label="序号" width="60" type="index"></el-table-column>
            <el-table-column prop="title" align="center" label="广告标题"></el-table-column>
            <el-table-column prop="imageUrl" label="广告图片">
                <template slot-scope="scope">
                    <el-image style="width: 90px; height: 60px"
                         :src="scope.row.imageUrl"
                          :preview-src-list="[scope.row.imageUrl]"></el-image>
                </template>
            </el-table-column>
            <el-table-column prop="advertUrl" label="广告链接"></el-table-column>
            <el-table-column prop="status" label="状态">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.status === 0">禁用</el-tag>
                    <el-tag v-if="scope.row.status === 1">正常</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="sort" align="center" label="排序"></el-table-column>
            <el-table-column  label="操作" >
                 <template slot-scope="scope">
                    <el-button type="success" @click="handleEdit(scope.row.id)" size="mini">编辑</el-button>
                    <el-button type="danger"  @click="handleDelete(scope.row.id)" size="mini">删除</el-button>
                </template>
            </el-table-column>
            
        </el-table>
        <!-- 分页功能 -->
         <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page.current"
            :page-sizes="[10, 20, 50]"
            :page-size="page.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total">
        </el-pagination>
        <edit :title="edit.title" :oldimageUrl="edit.oldimageUrl" :visible="edit.visible" :formData="edit.formData" :remoteClose="remoteClose"></edit>
</div>
</template>

<script>
import api from '@/api/advert'
import Edit from "./edit.vue"
const statusOptions = [
    {code:0,name:"禁用"},
    {code:1,name:"正常"}
]
export default { 
    name:"Advert",
    components:{
        Edit
    },
    data(){
        return {
           list:[],
           page:{
               total:0, //分页相关
               current:1,//总记录数
               size:20 //每页显示二十条数据
           },
           statusOptions,
            edit:{
                title:"",
                visible:false,
                formData:{
                    imageUrl:null
                },
                oldimageUrl:null //是编辑时，查询出来的原始图片
            },
           query:{}//查询条件
        }
    },
    created(){
        this.fetchData()
    },
    methods:{
        async fetchData(){
            const {data} = await api.getList(this.query,this.page.current,this.page.size)
            this.page.total = data.total
            this.list = data.records
        },
        //编辑
        async handleEdit(id){
            //通过id查询数据
            const res = await api.getById(id)
            if(res.code === 20000){
                this.edit.formData = res.data
                this.edit.oldimageUrl = res.data.imageUrl //修改前的照片URL
            }
            this.edit.title = "编辑"
            //弹出窗口
            this.edit.visible = true

        },
        //删除
        handleDelete(id){
             //删除            
            this.$confirm('确认删除该广告吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
            }).then(() => {
                //发送审核通过请求
               api.deleteById(id).then(res =>{
                   this.$message({
                       type:"success",
                       message:"删除成功"
                   })
               })
               //关闭弹窗
               this.fetchData()
            }).catch(() => {
                //取消删除
            });
        },
        //当前页
        handleSizeChange(val){
            this.page.size = val
            this.fetchData()
        },
        //每页多少条
        handleCurrentChange(val){
            this.page.current = val
            this.fetchData()
        },
        //条件查询
        queryData(){
            //将页码变为1，第一页
            this.page.current = 1
            this.fetchData()
        },
        //重置
        reload(){
            this.query = {}
            this.fetchData()
        },
        //新增打开窗口
        openAdd(){
            this.edit.visible = true
            this.edit.title = "新增"
        },
        //关闭窗口方法
        remoteClose(){
            this.edit.formData = {imageUrl:null}
            this.edit.visible = false
        }
    }
}
</script>

<style scoped>

</style>
